package com.anyisn.anyflickr.shell.model
{
	import air.update.ApplicationUpdaterUI;
	
	import flash.desktop.NativeApplication;
	import flash.events.ErrorEvent;
	import flash.filesystem.File;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.model.vo.ErrorVO;

	public class UpdateProxy extends Proxy
	{
		public static const NAME:String = "UpdateProxy";
		
		private var _updater:ApplicationUpdaterUI;
		private var _appXML:XML;
		private var _ns:Namespace;
		
		public function UpdateProxy(data:Object=null)
		{
			super(NAME, data);
			
			_appXML = NativeApplication.nativeApplication.applicationDescriptor;
			_ns = _appXML.namespace();
			
			_updater = new ApplicationUpdaterUI();
			_updater.configurationFile = new File("app:/config/update-config.xml");
			_updater.addEventListener(ErrorEvent.ERROR, onError);
			_updater.initialize();
		}
		
		public function checkForUpdate():void {
			_updater.checkNow();
		}
		
		public function get appName():String {
			return _appXML._ns::name;
		}
		
		public function get appVersion():String {
			return _appXML._ns::version;
		}
		
		private function onError(event:ErrorEvent):void {
			var error:ErrorVO = new ErrorVO(event.type, event.text, event.errorID);
			sendNotification(ShellFacade.ERROR, error);
		}
	}
}